package com.darly.chinese;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.darly.chinese.common.SpController;
import com.darly.dlcommon.framework.SpEnum;

import java.lang.ref.WeakReference;

/**
 * Description TODO: 监控是不是在前台展示、并切拿到当前Activity的实例对象。
 * Package com.darly.chinese
 * Author fronchdarly
 * Date 2021/6/3 1:41 PM
 */
public class ActivityLifeListener implements Application.ActivityLifecycleCallbacks {

    public int account = 0;

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

    }

    @Override
    public void onActivityStarted(Activity activity) {
        account++;
        SpController.getInstance().putValue(SpEnum.APP_FOREGROUND,account >0);
    }

    @Override
    public void onActivityResumed(Activity activity) {
        try {
            ChineseApplication.currentActivity = null;
            ChineseApplication.currentActivity = new WeakReference<>(activity);
            ChineseApplication.addActivity(activity);
        }catch (Exception e){

        }
    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        account--;
        if (account < 0) {
            account = 0;
        }
        SpController.getInstance().putValue(SpEnum.APP_FOREGROUND,account >0);
        ChineseApplication.removeActivity(activity);
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}
